<?php
// Copyright 2019 Hackware SpA <human@hackware.cl>
// This file is part of "Hackware Web Services Payment" and licensed under
// the terms of the GNU Affero General Public License version 3, or (at your
// option) a later version. You should have received a copy of this license
// along with the software. If not, see <https://www.gnu.org/licenses/>.

namespace Hawese\Payment\Gateways;

use Buzz\Client\FileGetContents as HttpClient;
use Nyholm\Psr7\Factory\Psr17Factory as Psr17ResponseFactory;
use Psr\Http\Message\ResponseInterface;

abstract class AbstractGateway
{
    protected $psr17Factory;
    protected $httpClient;

    public function __construct()
    {
        $this->psr17Factory = new Psr17ResponseFactory();
        $this->httpClient = new HttpClient($this->psr17Factory);
    }

    public function request(
        string $method,
        string $uri,
        array $bodyParams = [],
        array $headers = []
    ): ResponseInterface {
        $builderName = static::requestBuilderName();
        $requestBuilder = new $builderName(
            $method,
            $uri,
            $bodyParams,
            $headers
        );

        return $this->httpClient->sendRequest($requestBuilder->build());
    }

    protected static function requestBuilderName(): string
    {
        return substr(static::class, 0, -7) . 'RequestBuilder';
    }
}
